define([
    'zepto',
    'underscore',
    'backbone',
    "text!templates/qpay/unbind.html",
    "text!templates/qpay/addunbind.html",
    "wTools"

], function ($, _, Backbone, template,addVerify) {
    return Backbone.View.extend({
        $el: $main,
        el: $main0,
        bPass: false,
        isfirst:0,
        template: _.template(template),
        templateAddVerify: _.template(addVerify),
        events: {
            "tap #returnHome": "returnHome",
            "tap #getVerifyunbind": "getVerifyunbind",
            "tap #confirm_unbind": "confirmUnbind",
            "tap #cancel_btn": "cancel_btn",
            "tap #confirm_conBtn": "confirm_conBtn",
            "input #verifyNounbind":"verifyNounbind",
        },
        initialize: function () {
        	that = this;
        	$("#head_back").show();
            $("#home").html("");
            this.render();
            $("#addVerify").append(that.templateAddVerify());
            $("#returnHome").hide();
			$("#bindnumber").text(wData.bankNumber);
        },
		verifyNounbind:function(){
			var inp = $("#verifyNounbind").val();
			if(inp==''||inp==undefined||inp == null){
				$("#confirm_unbind").addClass("disabled")
			}else{
				$("#confirm_unbind").removeClass("disabled")
			}
		},
        //返回地址跳转
        returnHome: function () {
            location.hash = "home";
        },

        back: function () {
            location.hash = "qPay";
            location.reload();
            
        },
        render: function (data) {
            var html = this.template(data);
            this.$el.html(html);
        },
        destroy: function () {
            this.undelegateEvents();
            this.$el.html("");
            $('#home').html('');
            $("#head_back").show();
            delete this;
        },
        confirmUnbind:function(){
        	var val = $("#verifyNounbind").val();
        	if(val==null||val==undefined||val==""){
        		wTips("请输入验证码！")
        	}else{
        		$(".confirm_btn").addClass("show").removeClass("hide");
        	}
        },
        cancel_btn:function(){
        	$(".confirm_btn").addClass("hide").removeClass("show");
        },
        confirm_conBtn:function(){
        		$(".confirm_btn").addClass("hide").removeClass("show");
        		var val = $("#verifyNounbind").val();
        		var options = {
        			token : wData.token,
        			smsCode:val,
        			relCardOrderNo:wData.relCardOrderNo
        		}
        		wAjaxForm('unbundCardBySmsConfirm', '', options, function(result) {
        			console.log("解绑成功返回数据："+result)
        			result = JSON.parse(result);
        			if(result.bizStsCd=="1"){
        				location.hash = "qPay"
        				wTips(result.sysRtnDesc)
        				location.reload();
        			}else if(result.bizStsCd=="0"){
        				wTips(result.sysRtnDesc)
        			}
        		})        	
        },
        getVerifyunbind: function () {
        	//var Wnumber = wData.bankNumber;
        	var cardBegin = wData.bankNumber.substr(0,6),
        		cardEnd = wData.bankNumber.substr(wData.bankNumber.length-4);
        	
            if(this.bPass){
                return;
            }
            var bOK = $('#getVerifyunbind').hasClass('disabled' );
            if(bOK){
                return;
            }
            this.bPass = true;
            //objBlur();
            if(this.isfirst==0){
	            var postData = {
	            	token : wData.token,
	            	cardBegin:cardBegin,
	            	cardEnd:cardEnd
	            }
	            console.log(JSON.stringify(window.wData) )
	            var _that = this;
	//          var relCardOrderNo,
	//          	trxId;
	            wAjaxForm('unbundCardBySms', '', postData, function(result) {
	            	console.log("首次调用的接口"+ result)
	                _that.bPass = false;
	                result = JSON.parse(result);
	                if(result.bizStsCd == '1'){
	                	that.setTimer();
	                	$('#getVerifyunbind').addClass('disabled');
	                	wData.relCardOrderNo = result.relCardOrderNo;
	                	wData.trxId = result.trxId;
	                    //$('#chooseCard').addClass('disabled');
	                    
	                }else if(result.bizStsCd == '0'){
	                  	wTips("获取验证码失败，请重新获取！");
	                	wData.relCardOrderNo = result.relCardOrderNo;
	                	wData.trxId = result.trxId;
	                }
	            })
	          	this.isfirst++
            }else{
            	that.secondFun()
            }
			//console.log(this.isfirst)
        },
        secondFun:function(){
            var data = {
            		token : wData.token,
            		relCardOrderNo:wData.relCardOrderNo,
            		oriTrxId:wData.trxId
            	}
            var _that = this;
            wAjaxForm('unbundSmsResend', '', data, function(result) {
            	_that.bPass = false;
            	console.log("再次调用的接口"+ result)
            	result = JSON.parse(result);
            	if(result.bizStsCd == '1'){
            		$('#getVerifyunbind').addClass('disabled');
            		that.setTimer();
            	}else{
            		wTips("获取验证码失败，请重新获取！");
            	}
            	
            })        	
        },
        setTimer: function() {
            var sec = 60;
            var str = '';
            timer = setInterval(function() {
                sec--;
                if(sec==0){
                    clearInterval(timer);
                    str = '再次获取';
                    $('#getVerifyunbind').removeClass('disabled');
                    $('#chooseCard').removeClass('disabled');
                    
                }else{
                    str = sec+'s重新获取';
                }
                $('#getVerifyunbind').html(str);
            },1000);
        }
    });
});